#define DEBUG_BUFFERS 0

void dumpBufferfloat(float *d_buffer, int nelements)
{
	//FILE* fp;
	//errno_t err;

	unsigned int bytes = nelements*sizeof(float);
	float *h_buffer = (float *) malloc(bytes);
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);

	//if((err=fopen_s(&fp,"point.txt", "w"))==0)
	//{

	for(int i=0; i<nelements/3; i++) {
		//if(int(h_buffer[i])<0)
		//fprintf(fp, "%5d: %10f %10f %10f\n", i, h_buffer[6*i+0], h_buffer[6*i+1], h_buffer[6*i+2]);
		printf("%5d: %27.16e %27.16e %27.16e\n", i, h_buffer[3*i+0], h_buffer[3*i+1], h_buffer[3*i+2]);
		//if(i == 8)
		//	printf("");
	}
	//fclose(fp);
	//}
	//else
	//	printf("Error with open files!\n");
	//fprintf(fp, "	u,			v,			w\n" );	
	//fprintf(fp,"%25.16le %25.16le %25.16le\n", ppoint[0], ppoint[1], ppoint[2]);

	printf("\n");
	free(h_buffer);
}


void dumpBuffer(unsigned int *d_buffer, int nelements)
{
	printf("nelements=%d\n", nelements);
	unsigned int bytes = nelements*sizeof(unsigned int);
	unsigned int *h_buffer;
	h_buffer = (unsigned int *) (malloc(bytes));
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);

	//CUDA_SAFE_CALL(cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost) );
	printf("%d elements.\n", nelements);
	for(int i=0; i<nelements; i++) {
		if(int(h_buffer[i])>0)
		//printf("%d: %u, %u, %u \n", i, h_buffer[i]>>22, h_buffer[i]<<10>>22, h_buffer[i]<<20>>20);
		//printf("%d: %u\n", i, h_buffer[i]);
		printf("%d %u %u: %u\n", i, h_buffer[i]>>10, h_buffer[i]%1024, h_buffer[i]);
		//if (h_buffer[i]==13||h_buffer[i]<0||h_buffer[i]>26)
		if(i%50==0)
			printf(""); 
	} 
	printf("\n");

	free(h_buffer); 
}


void dumpBufferInt(int *d_buffer, int nelements)
{
	printf("nelements=%d\n", nelements);
	int bytes = nelements*sizeof(int);
	int *h_buffer;
	h_buffer = (int *) (malloc(bytes));
	cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost);

	//CUDA_SAFE_CALL(cudaMemcpy(h_buffer, d_buffer, bytes, cudaMemcpyDeviceToHost) );
	printf("%d elements.\n", nelements);
	for(int i=0; i<nelements; i++) {
		//if(int(h_buffer[i])>0)
		//printf("%d: %u, %u, %u \n", i, h_buffer[i]>>22, h_buffer[i]<<10>>22, h_buffer[i]<<20>>20);
		printf("%d: %d\n", i, h_buffer[i]);
		//printf("%d %u %u: %u\n", i, h_buffer[i]>>10, h_buffer[i]%1024, h_buffer[i]);
		//if (h_buffer[i]==13||h_buffer[i]<0||h_buffer[i]>26)
		if(i%50==0)
			printf(""); 
	} 
	printf("\n");

	free(h_buffer); 
}